package com.ruoyi.common.utils;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author sunh
 * @date 2024/8/23 16:59
 **/
public class EncryptionUtil {
    public static String PASSWORD = "JRWAPSbXoAgWLnUI";

    /**
     * 生成AES密钥
     *
     * @param password 密码
     * @return 密钥
     * @throws NoSuchAlgorithmException 密钥生成算法不支持异常
     */
    private static SecretKey generateAESKey(String password) throws NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] keyBytes = digest.digest(password.getBytes(StandardCharsets.UTF_8));
        return new SecretKeySpec(keyBytes, 0, 16, "AES"); // 只取前16个字节作为密钥
    }

    /**
     * 对称加密算法AES加密
     *
     * @param plaintext 明文
     * @param password  密码
     * @return 加密后的密文
     * @throws Exception 加密异常
     */
    public static String encryptWithAES(String plaintext, String password) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKey secretKey = generateAESKey(password);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    /**
     * 对称加密算法AES解密
     *
     * @param ciphertext 密文
     * @param password   密码
     * @return 解密后的明文
     * @throws Exception 解密异常
     */
    public static String decryptWithAES(String ciphertext, String password) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKey secretKey = generateAESKey(password);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(ciphertext));
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }

    public static void main(String[] args) {
        try {
            String password = PASSWORD;
            String plaintext = "{\n" +
                    "    \"messageContent\": \"通知\",\n" +
                    "    \"sendName\": \"发送人姓名\",\n" +
                    "    \"loginNames\": [\n" +
                    "        \"13596092242\"\n" +
                    "    ],\n" +
                    "    \"sendOrgName\": \"省厅\",\n" +
                    "    \"filePath\": \"\"\n" +
                    "}";
            // AES 加密
            String encryptedAES = EncryptionUtil.encryptWithAES(plaintext, password);
            System.out.println("AES 加密: " + encryptedAES);
            // AES 解密
            String decryptedAES = EncryptionUtil.decryptWithAES(encryptedAES, password);
            System.out.println("AES 解密: " + decryptedAES);
            Map map = new HashMap();
            map.put("data", encryptedAES);
//            String s = HttpUtils.sendPost("https://aqjdjczhgl.ajj.changchun.gov.cn/gsdss-api/si/api/message/v1/receive", JSONObject.toJSONString(map), new HashMap<>());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
